﻿using Acme.BookStore.Storage.DTO;
using AutoMapper.Internal.Mappers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Acme.BookStore.Storage
{
    public class InboundOperationLogAppService : ApplicationService, IInboundOperationLogAppService
    {
        private readonly IRepository<InboundOperationLog, Guid> _logRepository;

        public InboundOperationLogAppService(IRepository<InboundOperationLog, Guid> logRepository)
        {
            _logRepository = logRepository;
        }

        public async Task<PagedResultDto<InboundOperationLogDto>> GetListAsync(PagedAndSortedResultRequestDto input, string inboundManageId = null)
        {
            var queryable = await _logRepository.GetQueryableAsync();
            if (!string.IsNullOrEmpty(inboundManageId) && Guid.TryParse(inboundManageId, out var guid))
            {
                queryable = queryable.Where(x => x.InboundManageId == guid);
            }
            var logs = await AsyncExecuter.ToListAsync(queryable.PageBy(input));
            var totalCount = await _logRepository.CountAsync();
            return new PagedResultDto<InboundOperationLogDto>(
                totalCount,
                ObjectMapper.Map<List<InboundOperationLog>, List<InboundOperationLogDto>>(logs)
            );
        }
    }
}
